<?php
/**
 * 首页管理控制器
 * @author 阿柱 <422852954@qq.com>
 */
class IndexController extends CommonController{
    /**
	 * 默认首页
	 */
    public function index(){
//  	分配顶级分类
    	$this->topCate();
//		当日精选
		$today=S('today');
		if(!$today){
			$today=K('Goods')->order('rand()')->limit('4')->all();
			S('today',$today,60*60);
		}
		$this->assign('today',$today);
//		男装推荐
		$boys=S('boys');
		if(!$boys){
			$boys=$this->cidGetGoods(1,6);
			S('boys',$boys,60*60);
		}
		$this->assign('boys',$boys);
//		男装大图
		$this->assign('boysBig',K('Goods')->where("gid=45")->find());
//		女装推荐
		$girls=S('girls');
		if(!$girls){
			$girls=$this->cidGetGoods(2,6);
			S('girls',$girls,60*60);
		}
		$this->assign('girls',$girls);
//		女装大图
		$this->assign('girlsBig',K('Goods')->where("gid=46")->find());
//		配饰
		$peishi=S('peishi');
		if(!$peishi){
			$peishi=$this->cidGetGoods(3,8);
			S('peishi',$peishi,60*60);
		}
		$this->assign('peishi',$peishi);
//		童装分配
		$child=S('child');
		if(!$child){
			$child=$this->cidGetGoods(4,4);
			S('child',$child,60*60);
		}
		$this->assign('child',$child);
//		家居分配
		$home=S('home');
		if(!$home){
			$home=$this->cidGetGoods(5,4);
			S('home',$home,60*60);
		}
		$this->assign('home',$home);
//		分类品牌分类
		$brandData=K('Brand')->all();
		$this->assign('brandData',$brandData);
        //显示视图
        $this->display();
    }
	/**
	 * 通过顶级ID获得对应的商品
	 * param1 $cid 分类ID
	 * param $num 取的商品数量
	 */
	private function cidGetGoods($cid,$num){
		$allGids=K('Category')->cidGetGid($cid);
//		随机取六条
		$gidsKey=array_rand($allGids,$num);
		$data=array();
		foreach ($gidsKey as $key ) {
			$data[]=K('Goods')->where("gid={$allGids[$key]}")->find();
		}
		return $data;
		
	}
	
	
	
}
